package com.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.ClientMapper;
import com.pojo.Client;
import com.pojo.ClientExample;
import com.vo.QueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ClientService {
    
    @Resource
    private ClientMapper clientMapper;
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public Client queryByKey(int c_num){
        return clientMapper.selectByPrimaryKey(c_num);
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public List<Client> queryByType(Client client){
        ClientExample example = new ClientExample();
        ClientExample.Criteria criteria = example.createCriteria();
        if (client != null){
            if (client.getC_id() != null) {
                criteria.andC_idEqualTo(client.getC_id());
            }
            if (client.getC_name() != null && !"".equals(client.getC_name().trim())) {
                criteria.andC_nameEqualTo(client.getC_name().trim());
            }
            if (client.getC_phone1() != null && !"".equals(client.getC_phone1().trim())) {
                criteria.andC_phone1EqualTo(client.getC_phone1().trim());
            }
            if (client.getC_phone2() != null && !"".equals(client.getC_phone2().trim())) {
                criteria.andC_phone2EqualTo(client.getC_phone2().trim());
            }
            if (client.getC_address() != null && !"".equals(client.getC_address().trim())) {
                criteria.andC_addressLike("%"+client.getC_address().trim()+"%");
            }
        }
        return clientMapper.selectByExample(example);
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public PageInfo<Client> queryByPage(Integer pageNum , Integer pageSize , QueryVo vo){
        ClientExample example = new ClientExample();
        ClientExample.Criteria criteria = example.createCriteria();
        List<Client> clients = null;
        
        if (vo != null) {
            if (vo.getC_id() != null) {
                criteria.andC_idEqualTo(vo.getC_id());
            }
            if (vo.getC_name() != null && !"".equals(vo.getC_name().trim())) {
                criteria.andC_nameEqualTo(vo.getC_name().trim());
            }
            if (vo.getC_phone() != null && !"".equals(vo.getC_phone().trim())) {
                criteria.andC_phone1EqualTo(vo.getC_phone().trim());
                Client c = new Client();
                c.setC_phone2(vo.getC_phone().trim());
                clients = this.queryByType(c);
            }
        }
        PageHelper.startPage(pageNum , pageSize);
        List<Client> list = clientMapper.selectByExample(example);
        if (clients != null) {
            list.addAll(clients);
        }
        list.forEach(client -> System.out.println("\tclient : " + client));
        return new PageInfo<>(list);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int add(Client client){
        return clientMapper.insertSelective(client);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int update(Client client){
        return clientMapper.updateByPrimaryKeySelective(client);
    }
    
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int del(Integer c_num){
        return clientMapper.deleteByPrimaryKey(c_num);
    }
    
    
    
}
